package com.sise.wangzhan.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

/**
 * @Classname SecondCommand
 * @Description TODO    休眠期结束，hystrix尝试请求一次命令
 * @Date 2020/4/29 19:47
 * @Created by wangzhan
 */
public class SecondCommand extends HystrixCommand<String> {

    private boolean isTimeout;

    //设置超时时间为500毫秒
    public SecondCommand(boolean isTimeout) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGrop"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(500)));
        this.isTimeout = isTimeout;
    }

    @Override
    protected String run() throws Exception {

        //让外部决定是否超时
        if (isTimeout){
            //模拟超时  800 > 500
            Thread.sleep(800);
        }else {
            Thread.sleep(200);
        }
        return "";
    }

    @Override
    protected String getFallback(){
        return "";
    }
}
